﻿using System;
using System.Collections.Generic;
using IrrlichtLime;
using IrrlichtLime.Core;
using Timed;
using IrrlichtLime.Scene;
using IrrlichtLime.Video;
using IrrKlang;
using LuaInterface;
using System.Reflection;

namespace Iso
{
    public class Engine
    {
        // The main IrrlichtDevice, SoundEngine and LuaManager
        public static IrrlichtDevice device;
        public static ISoundEngine soundEngine;
        public static Dictionary<string, ISound> sounds;
        public static Lua lua;

        // Timeline
        public static uint then, now;
        public static float delta { get { now = device.Timer.Time; return (float)(now - then) / 1000.0f; } }
        public static uint currentTime { get { return device.Timer.RealTime; } }
        public static Timeline timeline;

        // Scene
        public static CameraSceneNode camera;
        public static IScene currentScene, nextScene;

        // Global Variables
        public static Dictionary<string, object> globalVariables;

        // Plugins
        public static Dictionary<string, Assembly> plugins;

        static void Main(string[] args)
        {
            string[] lines = "config.cfg".GetFileLines();
            string[] res = lines[0].Split('x');

            // Create the engine
            IrrlichtCreationParameters param = new IrrlichtCreationParameters();
            param.WindowSize = new Dimension2Di(int.Parse(res[0]), int.Parse(res[1])); // Risoluzione fissa :(
            param.DriverType = DriverType.Direct3D9;
            param.AntiAliasing = byte.Parse(lines[1]);
            param.Fullscreen = bool.Parse(lines[2]);
            param.BitsPerPixel = 32;
            device = IrrlichtDevice.CreateDevice(param);
            device.CursorControl.Visible = false;
            soundEngine = new ISoundEngine();
            soundEngine.SoundVolume = 0;
            sounds = new Dictionary<string, ISound>();
            lua = new Lua();
            globalVariables = new Dictionary<string, object>();
            plugins = new Dictionary<string, Assembly>();
            if (device != null)
            {
                Keyboard.Register(device);
                LuaHook.Register();
                Utility.PreloadGraphics();
                timeline = new Timeline();

                // Start LUA Script for game start
                //lua.DoFile("data\\scripts\\boot.lua");
                LoadScene(new VoxelTest());

                then = device.Timer.Time;
                while (device.Run())
                {
                    // Change scene
                    if (nextScene != null)
                    {
                        if (currentScene != null)
                            UnloadScene();
                        currentScene = nextScene;
                        currentScene.Load();
                        nextScene = null;
                    }

                    timeline.Check(device.Timer.RealTime);

                    // Update and Draw scene
                    currentScene.Update();
                    currentScene.Draw();

                    now = device.Timer.Time;
                    then = now;

                    soundEngine.Update();
                }

                // Unload all
                UnloadScene();
                device.Drop();
                soundEngine.Dispose();
                lua.Close();
            }
            else
                System.Windows.Forms.MessageBox.Show("Cannot initialize DirectX9 device!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }

        public static void LoadScene(IScene scene)
        {
            nextScene = scene;
        }

        public static void UnloadScene()
        {
            currentScene.Unload();
            currentScene = null;
        }
    }
}
